package com.xcity.game.employee.skill;

import java.text.MessageFormat;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Employee;
import com.xcity.game.skill.Skill;
import com.xcity.game.skill.template.SkillTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;

@Deprecated
public class EmployeeSkill implements Skill {

	protected int id;
	protected Float[] value;
	@JSONField(serialize = false)
	protected long lastTriggerTime;
	@JSONField(serialize = false)
	protected SkillTemplate template;
	@JSONField(serialize = false)
	protected Employee owner;
	
	public EmployeeSkill() {}
	
	public EmployeeSkill(SkillTemplate template) {
		this.id = template.getId();
		this.template = template;
		this.value = template.getInitVals();
	}

	@Override
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Float[] getValue() {
		if (getTemplate().isCanLevelUp()) {
			return value != null ? value : (value = getTemplate().getInitVals());
		}
		return getTemplate().getInitVals();
	}
	
	public float getValue(int i) {
		Float[] vals = getValue();
		return i >= 0 && i < vals.length ? vals[i] : 0f;
	}

	public void setValue(Float[] value) {
		this.value = value;
	}

	public long getLastTriggerTime() {
		return lastTriggerTime;
	}

	public void setLastTriggerTime(long lastTriggerTime) {
		this.lastTriggerTime = lastTriggerTime;
	}

	public Employee getOwner() {
		return owner;
	}

	public void setOwner(Employee owner) {
		this.owner = owner;
	}

	@Override
	public SkillTemplate getTemplate() {
		return template != null ? template : (template = App.getApp().getServiceManager().get(TemplateService.class).get(SkillTemplate.class, id));
	}

	public void setTemplate(SkillTemplate template) {
		this.template = template;
	}
	
	@Override
	public String getName() {
		return getTemplate().getName();
	}
	
	@Override
	public String getDesc() {
		String desc = getTemplate().getDesc();
		Object[] vals = getValue();
		if (vals != null && vals.length > 0) {
			desc = MessageFormat.format(desc, vals);
		}
		return desc;
	}

	public void addLevel(int val) {
		if (getTemplate().isCanLevelUp()) {
			Float[] vals = getValue();
			for (int i = 0, len = vals.length; i < len; i++) {
				vals[i] += getTemplate().getGrowthVal(i);
			}
		}
	}
	
	@JSONField(serialize = false)
	public boolean isInCD() {
		int cd;
		return lastTriggerTime != 0 && (cd = getTemplate().getCd() * 1000) != 0 && lastTriggerTime + cd < TimeUpdater.getInstance().now();
	}
	
	public byte getType() {
		return getTemplate().getType();
	}
	
	public int getCd() {
		return getTemplate().getCd() * 1000;
	}
	
	@Override
	public String getIcon() {
		return getTemplate().getIcon();
	}

	@Override
	public boolean addLevel(int val, String cause) {
		return false;
	}

	@Override
	public float getParameter(int index) {
		return getValue(index);
	}

}
